/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.buss.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间格式操作类
 * @author Administrator
 */
public class DateUtil {

	public static final String FORMAT_DATE = "yyyy-MM-dd";
	public static final String FORMAT_TIME = "HH:mm:ss";
	public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 返回当前日期,格式为yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getCurrentDateStr() {
		return new SimpleDateFormat(FORMAT_DATE).format(new Date());
	}

	/**
	 * 返回当前日期
	 * 
	 * @param format
	 *            <自定义的日期格式>
	 * @return
	 */
	public static String getCurrentDate(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 返回当前时间,格式为HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		return new SimpleDateFormat(FORMAT_TIME).format(new Date());
	}

	/**
	 * 获取当前时间
	 * 
	 * @param format
	 *            <用户自定义的时间格式>
	 * @return
	 */
	public static String getCurrentTime(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

        //获取当前时间的毫秒数
        public static Long getCurrentLongTime(){
            Calendar cal = Calendar.getInstance();
            return cal.getTimeInMillis();
        }
	/**
	 * 返回当前日期时间，格式为yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getCurrentDateTime() {
		return getCurrentDateTime(FORMAT_DATETIME);
	}

	/**
	 * 获取当前日期和时间
	 * 
	 * @param format
	 *            <用户自己定义格式>
	 * @return
	 */
	public static String getCurrentDateTime(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 返回当前星期
	 * 
	 * @return
	 */
	public static int getDayOfWeek() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 根据传入的日期返回星期
	 * 
	 * @param date
	 *            <用户传入的日期>
	 * @return
	 */
	public static int getDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 返回当前日期是该月中第几天
	 * 
	 * @return
	 */
	public static int getDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回指定日期是月中第几天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回指定日期最大天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getMaxDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 返回当前日期最大天数
	 * 
	 * @return
	 */
	public static int getMaxDayOfMonth() {
		return getMaxDayOfMonth(new Date());
	}

	/**
	 * 返回指定日期月份的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String getFirstDateOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat(FORMAT_DATE).format(cal.getTime());
	}

	/**
	 * 返回当前日期是一年中的第几天
	 * 
	 * @return
	 */
	public static int getDayOfYear() {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.DAY_OF_YEAR);
	}

         /**
     * 对传入的日期进行格式化处理
     * 默认格式为yyyy-MM-dd
     * @param date
     * @return
     */
    public static String getDateFormat(Date date){
        return getDateFormat(date, FORMAT_DATE);
    }

    /**
     * 对传入的日期进行格式化处理
     * @param date
     * @param format
     * @return
     */
    public static String getDateFormat(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

	/**
	 * 返回指定日期是一年中的第几天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	public static int getDayOfWeek(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(Calendar.DAY_OF_WEEK);
	}

	public static int getDayOfMonth(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	public static int getDayOfYear(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(parse(date));
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	public static int compare(String date1, String date2) {
		return compare(date1, date2, FORMAT_DATE);
	}

	public static int compareTime(String time1, String time2) {
		return compareTime(time1, time2, FORMAT_TIME);
	}

	public static int compare(String date1, String date2, String format) {
		Date d1 = parse(date1, format);
		Date d2 = parse(date2, format);
		return d1.compareTo(d2);
	}

	public static int compareTime(String time1, String time2, String format) {
		String[] arr1 = time1.split(":");
		String[] arr2 = time2.split(":");
		if (arr1.length < 2) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if (arr2.length < 2) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		int h1 = Integer.parseInt(arr1[0]);
		int m1 = Integer.parseInt(arr1[1]);
		int h2 = Integer.parseInt(arr2[0]);
		int m2 = Integer.parseInt(arr2[1]);
		int s1 = 0;
		int s2 = 0;
		if (arr1.length == 3) {
			s1 = Integer.parseInt(arr1[2]);
		}
		if (arr2.length == 3) {
			s2 = Integer.parseInt(arr2[2]);
		}
		if ((h1 < 0) || (h1 > 23) || (m1 < 0) || (m1 > 59) || (s1 < 0)
				|| (s1 > 59)) {
			throw new RuntimeException("错误的时间值:" + time1);
		}
		if ((h2 < 0) || (h2 > 23) || (m2 < 0) || (m2 > 59) || (s2 < 0)
				|| (s2 > 59)) {
			throw new RuntimeException("错误的时间值:" + time2);
		}
		if (h1 != h2) {
			return ((h1 > h2) ? 1 : -1);
		}
		if (m1 == m2) {
			if (s1 == s2) {
				return 0;
			}
			return ((s1 > s2) ? 1 : -1);
		}
		return ((m1 > m2) ? 1 : -1);
	}

	/**
	 * 是否为时间格式
	 * 
	 * @param time
	 * @return
	 */
	public static boolean isTime(String time) {
		String[] arr = time.split(":");
		if (arr.length < 2) {
			return false;
		}
		try {
			int h = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int s = 0;
			if (arr.length == 3) {
				s = Integer.parseInt(arr[2]);
			}
			if ((h < 0) || (h > 23) || (m < 0) || (m > 59) || (s < 0)
					|| (s > 59)) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 是否为日期，格式必须为yyyy-MM-dd
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isDate(String date) {
		String[] arr = date.split("-");
		if (arr.length < 3) {
			return false;
		}
		try {
			int y = Integer.parseInt(arr[0]);
			int m = Integer.parseInt(arr[1]);
			int d = Integer.parseInt(arr[2]);
			if ((y < 0) || (m > 12) || (m < 0) || (d < 0) || (d > 31)) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 将指定字符串类型转换为日期类型 转换后格式为:yyyy-MM-dd
	 * 
	 * @param str
	 * @return
	 */
	public static Date parse(String str) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		try {
			return new SimpleDateFormat(FORMAT_DATE).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将字符串类型转换为日期类型
	 * 
	 * @param str
	 *            <要转换的字符串>
	 * @param format
	 *            <转换后的格式>
	 * @return
	 */
	public static Date parse(String str, String format) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);

			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

        /**
         * 将LONG转化为日期格式
         * @param time
         * @param format
         * @return
         */
        public static Date parse(Long time){
            if(time == null){
                return null;
            }
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(time);
            return cal.getTime();
        }

	/**
	 * 将指定字符类型转换位日期时间类型 格式为:yyyy-MM-dd HH:mm:ss
	 * 
	 * @param str
	 *            <要转换的字符串>
	 * @return
	 */
	public static Date parseDateTime(String str) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		if (str.length() <= 10) {
			return parse(str);
		}
		try {
			return new SimpleDateFormat(FORMAT_DATETIME).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将自定字符串转为日期类型
	 * 
	 * @param str
	 *            <要转换的字符串>
	 * @param format
	 *            <用户自定义日期格式>
	 * @return
	 */
	public static Date parseDateTime(String str, String format) {
		if (StringUtil.isEmpty(str)) {
			return null;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 给指定时间增加分钟
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addMinute(Date date, int count) {
		return new Date(date.getTime() + 60000L * count);
	}

	/**
	 * 给指定时间增加小时数
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addHour(Date date, int count) {
		return new Date(date.getTime() + 3600000L * count);
	}

	/**
	 * 给指定时间增加天数
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addDay(Date date, int count) {
		return new Date(date.getTime() + 86400000L * count);
	}

        /**
         * 给指定的时间增加天数，放回不带时间的日期
         * @param date
         * @param count
         * @return
         */
        public static Date addDayNoTime(Date date,int count){
            return addDay(parse(getDateFormat(date)),1);
        }

	/**
	 * 给指定时间增加周数
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addWeek(Date date, int count) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.WEEK_OF_YEAR, count);
		return cal.getTime();
	}

	/**
	 * 给指定时间增加月数
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addMonth(Date date, int count) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, count);
		return cal.getTime();
	}

	/**
	 * 给指定时间增加年数
	 * 
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date addYear(Date date, int count) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, count);
		return cal.getTime();
	}

	/**
	 * 获取两个日期的间隔天数
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws GeneralException
	 */
	public static long getDaysBetween(Date startDate, Date endDate) {
		Calendar startCal = GregorianCalendar.getInstance();
		Calendar endCal = GregorianCalendar.getInstance();                
		startCal.setTime(startDate);
		endCal.setTime(endDate);
                long sDate = startCal.getTimeInMillis() / (24 * 60 * 60 * 1000);
                long eDate = endCal.getTimeInMillis() / (24 * 60 * 60 * 1000);
		return sDate - eDate;
	}

	public static void main(String[] args) {
		Calendar startCal = GregorianCalendar.getInstance();
		startCal.add(Calendar.DAY_OF_YEAR, -400);
		long aa = DateUtil.getDaysBetween(startCal.getTime(), new Date());
		System.out.println(aa);
	}
}
